export const debounce = function (fn, delay = 300) {
  let Timer
  return function () {
    let _this = this
    if (Timer) {
      clearTimeout(Timer)
    }
    Timer = setTimeout(() => {
      Timer = null
      fn.call(_this, ...arguments)
    }, delay)
  }
}

export const throttle = function (fn, delay = 600) {
  let startTime = new Date()

  return function () {
    let currentTime = new Date()
    let _this = this

    if (currentTime - startTime >= delay) {
      fn.call(_this, ...arguments)
      startTime = currentTime
    }
  }
}

export const sortByName = function () {
  return function (a, b) {
    if (
      a.room_name &&
            b.room_name &&
            typeof a.room_name === 'string' &&
            typeof b.room_name === 'string'
    ) {
      return a.room_name.toUpperCase() <
                b.room_name.toUpperCase()
        ? -1
        : 1
    } else {
      return 0
    }
  }
}

export const concatLocation = function (location, deep = 4, split = ' ') {
  let result = ''
  let count = deep
  const keys = ['province', 'city', 'district', 'address']
  while (count > 0) {
    if (count === deep) {
      // 如果省和城市相同, 不需要省
      result += location[keys.shift()] || ''
      if (location.province === location.city) {
        keys.shift()
        count--
      }
    } else {
      result += split + (location[keys.shift()] || '')
    }
    count--
  }
  return result
}

export const isArray = function(value) {
    return Object.prototype.toString.call(value) === '[object Array]'
}

export const getRoomName = function(room) {
    if (room.room_type === RoomTypeEnum.PUBLIC) {
        if (!room.parent_ids || !room.parent_ids.length) {
            return '公区'
        }
        return room.room_name + '公区'
    }
    return room.room_name
}

export const sayHello = function () {
    console.log('sayHello')
}

export const sayName = function () {
    console.log('sayName')
}